//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.bookingsetlist;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.PickableBean;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.bookingset.BookingSet;
import com.restaurant.application.bookingset.support.BookingSetData;
import com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListBean;
import com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListCriteria;
import com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListImpl;
import com.restaurant.application.indexes.bookingsetlist.support.AbstractAssistBookingSetList;
import com.restaurant.application.indexes.bookingsetlist.support.SearchBookingData;
import com.restaurant.application.minifacade.data.BookingBO;
import com.restaurant.application.minifacade.data.BookingTime;
import com.restaurant.application.minifacade.data.ShopBO;
import com.restaurant.application.minifacade.data.ShopBookingTime;
import com.restaurant.application.shop.Shop;
import com.restaurant.application.shop.support.ShopData;
import com.restaurant.application.util.CommonUtils;
import com.restaurant.application.util.GetDateUtil;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class AssistBookingSetListImpl extends AbstractAssistBookingSetList {
    public AssistBookingSetListImpl() {
    }

    public ViewUpdate create(int[] seq) {
        BookingSet bookingSet = (BookingSet)this.createNode(BookingSet.class);
        return this.returnPushInContainer(bookingSet.getId(), false);
    }

    public ViewUpdate edit(int[] seq) {
        BookingSetListBean bean = this.findOne(seq);
        if (bean == null) {
            this.getLogger().error("not find this booking");
            return this.returnView(new View[]{this.getFullView()});
        } else {
            return this.returnPushInContainer(bean.getBookingSet(), false);
        }
    }

    public ViewUpdate delete(int[] seq) {
        ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).delete((long)seq[0]);
        return this.returnView(new View[]{this.getFullView()});
    }

    private BookingSetListBean findOne(int[] seq) {
        List<BookingSetListBean> beans = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search((new BookingSetListCriteria()).withRecordId((long)seq[0]), (SortSpec)null, 2);
        return beans.size() > 0 ? (BookingSetListBean)beans.get(0) : null;
    }

    public ViewUpdate search(int[] seq) {
        return null;
    }

    public ViewUpdate switchPage(SearchBookingData searchData, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(searchData)});
    }

    public View getFullView() {
        return this.newCommonTable(new SearchBookingData());
    }

    public View getViewLead(ViewSpec spec) {
        return null;
    }

    public BookingBO findBookingShops() {
        List<BookingSetListBean> search = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search(new BookingSetListCriteria(), (SortSpec)null, 1000);
        Set<UID> shopSet = (Set)search.stream().map(BookingSetListBean::getShop).collect(Collectors.toSet());
        List<ShopBO> boList = (List)shopSet.stream().map((e) -> {
            ShopData shopData = ((Shop)this.getUnit(Shop.class, e)).findData();
            return (new ShopBO()).withShopId(shopData.getId()).withShopName(shopData.getName());
        }).collect(Collectors.toList());
        List<String> list = (List)search.stream().map(BookingSetListBean::getBookingSet).flatMap((e) -> {
            BookingSetData bData = ((BookingSet)this.getUnit(BookingSet.class, e)).findData();
            return bData.getService().stream();
        }).distinct().collect(Collectors.toList());
        return (new BookingBO()).withShops(boList).withService(list);
    }

    public List<ShopBookingTime> findBookingSetListByService(String service) {
        List<BookingSetListBean> search = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search((new BookingSetListCriteria()).withServiceLike(service), (SortSpec)null, 500);
        Map<String, List<BookingSetData>> collect = (Map)search.stream().map((e) -> {
            return ((BookingSet)this.getUnit(BookingSet.class, e.getBookingSet())).findData();
        }).collect(Collectors.groupingBy(BookingSetData::getShopId, Collectors.toList()));
        List<ShopBookingTime> list2 = (List)collect.entrySet().stream().map((e) -> {
            String k = (String)e.getKey();
            List<BookingSetData> v = (List)e.getValue();
            ShopBookingTime time = new ShopBookingTime();
            time.withShopId(k).withShopName(((BookingSetData)v.get(0)).getShopName());
            List<BookingTime> collect1 = this.getBookingTimes(v);
            return time.withBookingTime(collect1);
        }).collect(Collectors.toList());
        return list2;
    }

    public ShopBookingTime findBookingSetList(String shopId, String service) {
        List<BookingSetListBean> search = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search((new BookingSetListCriteria()).withShopIdEquals(shopId), (SortSpec)null, 500);
        List<BookingSetData> collect = (List)search.stream().map((e) -> {
            return ((BookingSet)this.getUnit(BookingSet.class, e.getBookingSet())).findData();
        }).collect(Collectors.toList());
        List<String> services = (List)collect.stream().flatMap((e) -> {
            return e.getService().stream();
        }).distinct().collect(Collectors.toList());
        if (!Strings.isNullOrEmpty(service)) {
            search = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search((new BookingSetListCriteria()).withShopIdEquals(shopId).withServiceLike(service), (SortSpec)null, 500);
            collect = (List)search.stream().map((e) -> {
                return ((BookingSet)this.getUnit(BookingSet.class, e.getBookingSet())).findData();
            }).collect(Collectors.toList());
        }

        return (new ShopBookingTime()).withBookingTime(this.getBookingTimes(collect)).withServices(services);
    }


    public String checkIsRepeat(UID booking, BookingSetData setData) {
        List<BookingSetListBean> search = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search((new BookingSetListCriteria()).withBookingSetNotEquals(booking).withShopNameEquals(setData.getShopName()), (SortSpec)null, 500);
        List<BookingSetData> collect = (List)search.stream().map((e) -> {
            return ((BookingSet)this.getUnit(BookingSet.class, e.getBookingSet())).findData();
        }).collect(Collectors.toList());
        List<BookingTime> times = this.getBookingTimes(collect);
        BookingTime bookingTime = (BookingTime)times.stream().filter((e) -> {
            return LocalDate.parse(e.getDate()).compareTo(LocalDate.parse(setData.getStartDate())) >= 0 && LocalDate.parse(e.getDate()).compareTo(LocalDate.parse(setData.getEndDate())) <= 0;
        }).findFirst().orElse(null);
        return bookingTime == null ? null : (String)bookingTime.getTime().stream().map((e) -> {
            String[] split = e.split("-");
            String start = split[0];
            String end = split[1];
            String s = (String)setData.getTimes().stream().filter((x) -> {
                return this.mess(start, end, x.getStartTime(), x.getEndTime());
            }).map((m) -> {
                return "与其他设置冲突";
            }).findFirst().orElse("");
            return s;
        }).findFirst().orElse("");
    }

    private boolean mess(String start, String end, String sTime, String eTime) {
        LocalTime startTime = LocalTime.parse(start);
        LocalTime endTime = LocalTime.parse(end);
        LocalTime pTime = LocalTime.parse(sTime);
        LocalTime peTime = LocalTime.parse(eTime);
        return endTime.compareTo(pTime) >= 0 && peTime.compareTo(startTime) >= 0;
    }

    private List<BookingTime> getBookingTimes(List<BookingSetData> collect) {
        Map<String, BookingTime> b = new LinkedHashMap();
        collect.forEach((x) -> {
            List<String> list = GetDateUtil.getBetweenDate(x.getStartDate(), x.getEndDate());
            List<String> list1 = (List)x.getTimes().stream().map((p) -> {
                return p.getStartTime() + "-" + p.getEndTime();
            }).collect(Collectors.toList());
            list.stream().filter((p) -> {
                return LocalDate.parse(p).compareTo(LocalDate.now()) >= 0;
            }).forEach((m) -> {
                BookingTime bookingTime;
                if (b.containsKey(m)) {
                    bookingTime = (BookingTime)b.get(m);
                    bookingTime.getTime().addAll(list1);
                    bookingTime.getService().addAll(x.getService());
                } else {
                    bookingTime = new BookingTime();
                    bookingTime.withDate(m).withTime(list1).withService(x.getService());
                    b.put(m, bookingTime);
                }

            });
        });
        return new ArrayList(b.values());
    }

    private View newCommonTable(SearchBookingData searchData) {
        List<BookingSetListBean> beans = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).search(new BookingSetListCriteria(), CommonUtils.getSort(), 0, 20);
        PageForm pageForm = this.newFormShell(beans, searchData).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    private FormShell newFormShell(List<BookingSetListBean> beans, SearchBookingData searchData) {
        FormShell shell = new FormShell("预约/预约设置", 3, 2);
        if (!Strings.isNullOrEmpty(this.getPath(CMD_CREATE, new int[0]))) {
            shell.at(1, 1, "").buttonBoxField(new ButtonField[]{new ButtonField(this.getPath(CMD_CREATE, new int[0]), "新增", false, (String)null, false)});
        }

        TableShell<BookingSetListBean> tableShell = new TableShell(BookingSetListBean.class, beans);
        int pageSize = 10;
        int pageNum = 1;
        int totalRecord = ((BookingSetListImpl)this.getIndex(BookingSetListImpl.class)).count(new BookingSetListCriteria());
        int totalPage = (totalRecord + pageSize - 1) / pageSize;
        PageInfo pageInfo = (new PageInfo()).withPageNum(Integer.valueOf(pageNum)).withPageSize(Integer.valueOf(pageSize)).withTotalPage(totalPage).withTotalRecord(totalRecord).withJumpPath(this.getPath(CMD_SWITCHPAGE, new int[0])).withPageNumberField("selectedPage");
        tableShell.withPageInfo(pageInfo);
        tableShell.createColumn("shopName").withName("门店").withWidth(150).cellCalculator((e) -> {
            return this.getSimpleCell(e, "shopName");
        });
        tableShell.createColumn("dates").withName("日期").withWidth(200).cellCalculator((e) -> {
            return this.getSimpleCell(e, "dates");
        });
        tableShell.createColumn("times").withName("时间段").cellCalculator((e) -> {
            return this.getSimpleCell(e, "times");
        });
        tableShell.createColumn("service").withName("服务").cellCalculator((e) -> {
            return this.getSimpleCell(e, "service");
        });
        tableShell.createColumn("doSomething").withName("操作").withWidth(200).cellCalculator((e) -> {
            BookingSetListBean bean = (BookingSetListBean)e;
            List<ButtonField> fields = Lists.newArrayList();
            if (!Strings.isNullOrEmpty(this.getPath(CMD_EDIT, new int[0]))) {
                fields.add((new ButtonField()).withButtonPath(this.getPath(CMD_EDIT, new long[]{bean.getRecordId()})).withWithData(false).withLabel("编辑"));
            }

            if (!Strings.isNullOrEmpty(this.getPath(CMD_DELETE, new int[0]))) {
                fields.add((new ButtonField()).withButtonPath(this.getPath(CMD_DELETE, new long[]{bean.getRecordId()})).withWithData(false).withLabel("删除"));
            }

            return fields.size() > 0 ? (new FieldValue()).withButtonBox(fields).withEntryType(EntryTypes.BUTTONBOX) : (new FieldValue()).withValue("");
        });
        shell.at(3, 0, "预约/预约设置").large(3, 1).tableField("bookingList", tableShell);
        return shell;
    }

    private FieldValue getSimpleCell(PickableBean bean, String field) {
        return (new FieldValue()).withValue(String.valueOf(bean.getValueOf(field)));
    }
}
